<?php

namespace App\Console\Commands;

use App\Model\buyDlt;
use App\Model\dlt;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class sendDltWin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sendDltWin';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $res = (new dlt())->getLastQh();
        $qihao = $res->qihao;
        $number = $res->number;

        $winResObj = (new buyDlt())->getLastQh($qihao);

        $len = count($winResObj);

        if($len > 0){
            foreach ($winResObj as $buyObj) {
                $buyArr = $buyObj->toArray();
                if($buyArr['no1']){
                    $reward = $this->compareDlt($buyArr['no1'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no2']){
                    $reward = $this->compareDlt($buyArr['no2'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no3']){
                    $reward = $this->compareDlt($buyArr['no3'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no4']){
                    $reward = $this->compareDlt($buyArr['no4'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }

                if($buyArr['no5']){
                    $reward = $this->compareDlt($buyArr['no5'],$number);
                    if($reward){
                        $this->sendMail($reward);
                    }
                }
            }
        }
        if($res->flag != 1){
            dlt::where(['qihao'=>$qihao])->update(['flag'=>1]);
        }
    }

    private function compareDlt($officialNumbers, $userNumbers){
        // 分割官方号码和用户号码
        $officialParts = explode('|', $officialNumbers);
        $userParts = explode('|', $userNumbers);

        // 校验格式：官方号码应该是 7 个数字（前区5 + 后区2），用户同理
        if (count($officialParts) !== 7 || count($userParts) !== 7) {
            return 0; // 格式不对，默认不中
        }

        // 提取前区和后区号码（字符串转整型，去重排序不影响比较）
        $officialFront = array_slice($officialParts, 0, 5); // 前区 5 个
        $officialBack = array_slice($officialParts, 5, 2);  // 后区 2 个

        $userFront = array_slice($userParts, 0, 5);
        $userBack = array_slice($userParts, 5, 2);

        // 转为整数，并去除可能的重复/无效字符（简单过滤）
        $officialFront = array_map('intval', $officialFront);
        $officialBack = array_map('intval', $officialBack);
        $userFront = array_map('intval', $userFront);
        $userBack = array_map('intval', $userBack);

        // 计算前区命中个数
        $frontMatchCount = 0;
        foreach ($userFront as $num) {
            if (in_array($num, $officialFront)) {
                $frontMatchCount++;
            }
        }

        // 计算后区命中个数
        $backMatchCount = 0;
        foreach ($userBack as $num) {
            if (in_array($num, $officialBack)) {
                $backMatchCount++;
            }
        }

        // 根据前区 + 后区命中数判断奖项
        $front = $frontMatchCount;
        $back = $backMatchCount;

        if ($front == 5 && $back == 2) {
            return 1; // 一等奖
        } elseif ($front == 5 && $back == 1) {
            return 2; // 二等奖
        } elseif ($front == 5 && $back == 0) {
            return 3; // 三等奖
        } elseif ($front == 4 && $back == 2) {
            return 4; // 四等奖
        } elseif ($front == 4 && $back == 1) {
            return 5; // 五等奖
        } elseif ($front == 4 && $back == 0) {
            return 6; // 六等奖
        } elseif ($front == 3 && $back == 2) {
            return 7; // 七等奖
        } elseif (($front == 3 && $back == 1) || ($front == 2 && $back == 2)) {
            return 8; // 八等奖
        } elseif (
            ($front == 3 && $back == 0) ||
            ($front == 1 && $back == 2) ||
            ($front == 2 && $back == 1) ||
            ($front == 0 && $back == 2) ||
            ($front == 1 && $back == 1) ||
            ($front == 0 && $back == 1) ||
            ($front == 0 && $back == 0)
        ) {
            return 9; // 九等奖（符合一些常见组合，根据实际规则可调整）
        } else {
            return 0; // 未中奖
        }
    }

    private function sendMail($reward){
        $res = (new dlt())->getLastQh();
        $flag = $res->flag;
        if($flag == 0){
            if($reward == 1){
                for ($i=0; $i < 100; $i++) {
                    //发邮件通知
                    Mail::raw('牛逼了，无敌了！！！！！！！！！！！', function($message){
                        $message->to('171161005@qq.com');
                        $message->subject('牛逼了，无敌了!!!!!!!!!!!!!!!!!!');
                    });
                }
            }elseif ($reward == 2) {
                for ($i=0; $i < 3; $i++) {
                    //发邮件通知
                    Mail::raw('草！就差1下！', function($message){
                        $message->to('171161005@qq.com');
                        $message->subject('草啊!就差1下下!');
                    });
                }
            }elseif ($reward == 3) {
                //发邮件通知
                Mail::raw('3', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('3,10000');
                });
            }elseif ($reward == 4) {
                //发邮件通知
                Mail::raw('4', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('4,3000');
                });
            }elseif ($reward == 5) {
                //发邮件通知
                Mail::raw('5', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('5,300');
                });
            }elseif ($reward == 6){
                //发邮件通知
                Mail::raw('6', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('6,200');
                });
            }elseif ($reward == 7){
                //发邮件通知
                Mail::raw('7', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('7,100');
                });
            }elseif ($reward == 8){
                //发邮件通知
                Mail::raw('8', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('8,15');
                });
            }elseif ($reward == 9){
                //发邮件通知
                Mail::raw('9', function($message){
                    $message->to('171161005@qq.com');
                    $message->subject('9,5');
                });
            }else{

            }
        }
    }
}
